#include "wds_pickup_manager.h"
#include "wds_pickup_collection.h"
#include "wds_player.h"
#include "gen_game_state.h"

#include "num/rand.h"


WDSPickupManager::WDSPickupManager() : m_pPlayer(NULL)
{
	GENGameState* pState = GENGameState::GetGame();
	if(pState)
	{
		WDSGame* pGame = pState->GetGameWDS();
		if(pGame)
		{
			m_pPlayer = pGame->GetPlayer();
		}
	}
}

WDSPickupManager&		WDSPickupManager::Instance()
{
	static WDSPickupManager	Inst;
	return Inst;
}


void	WDSPickupManager::Render(const math::Vec2& Offset,float Scale)
{
	 for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		pCol->Render(Offset,Scale);
	}
	
	//FIGPickupAnimation::Instance().Render(Offset,Scale);
}

void	WDSPickupManager::RenderDebug(const math::Vec2& Offset,float Scale)
{
	 for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		pCol->RenderDebug(Offset,Scale);
	}
	//FIGPickupAnimation::Instance().RenderDebug(Offset,Scale);
}

void	WDSPickupManager::RenderShadow(const math::Vec2& Offset,float Scale)
{
	 for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		pCol->RenderShadow(Offset,Scale);
	}
}

void	WDSPickupManager::Tick(float dt)
{
    for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		pCol->Tick(dt);
	}

	//FIGPickupAnimation::Instance().Tick(dt);
}


void	WDSPickupManager::Clear()
{
    for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		pCol->Clear();
		UTIL_SAFE_DELETE(pCol);
	}
	m_PickupCollections.resize(0);
}

void	WDSPickupManager::Spawn(const std::string& TypeName,
								const math::Vec2& Pos
											)
{
	WDSPickup::PICKUP_TYPES Type = WDSPickup::MapNameToType(TypeName);
	Spawn(Type,Pos);
}

void	WDSPickupManager::Spawn(WDSPickup::PICKUP_TYPES Type, 
											const math::Vec2& Pos
										)
{
	bool Found = false;
	for( int i = (int)m_PickupCollections.size() - 1; i >= 0; i--)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		if ( pCol->GetType() == Type)
		{
			pCol->Spawn(Type , Pos);
			Found = true;
			i = -1;
		}		
	}

	if(!Found)
	{
		WDSPickupCollection* pCol = new WDSPickupCollection(Type);
		m_PickupCollections.push_back(pCol);
		pCol->Spawn(Type , Pos);
	}
}

WDSPickup::PICKUP_TYPES	WDSPickupManager::HitTest(const math::Vec2& P, float Radius,  float Scale)
{
	WDSPickup::PICKUP_TYPES PickupType;
	for(unsigned int i = 0; i < m_PickupCollections.size(); i++)
	{
		WDSPickupCollection* pCol = m_PickupCollections[i];
		PickupType = pCol->HitTest(P, Radius, Scale);
		if(PickupType > WDSPickup::PICKUP_TYPE_NONE)
			return PickupType;
	}
	return WDSPickup::PICKUP_TYPE_NONE;
}

void	WDSPickupManager::SpawnChance(const math::Vec2& Pos)
{
	// logic:
	// if player is using a pistol and if there is no weapon_pickup on the game , spawn a weaspon_pickup
	// if there is a already spawned weapon_pickup on the map, don't spawn weapon_pickup
	// if player is using an advance weapon , reduce propability of spawning a weapon
	// if random spawn weapon pickup is locked, spawn the other weapon pickup.
	// OR
	// spawn other pickup with propability
	
	if(!m_pPlayer)
		return;
	
	const int MAX_RAND_V = 20;
	const int WEAPON_PROPABILITY = 9;
	WDSPickup::PICKUP_TYPES PickupTYpe = WDSPickup::PICKUP_TYPE_NONE;
	float BonusMagnetSkillUpgrade = m_pPlayer->GetSkill()->m_BonusMagnet;

	int RandVal = num::RAND32::Instance().RandInt(0,  (int)((float)MAX_RAND_V * BonusMagnetSkillUpgrade) );

	if(RandVal == 0)
	{
		if( RandVal = num::RAND32::Instance().RandInt( 0 , WEAPON_PROPABILITY) == 0)
		{
			// spawn weapon
			// check locked????
			RandVal = num::RAND32::Instance().RandInt( ((int)WDSPickup::PICKUP_TYPE_GUNSTART) + 1 , ((int)WDSPickup::PICKUP_TYPE_GUNEND) - 1);
		}
		else
		{
			RandVal = num::RAND32::Instance().RandInt( ((int)WDSPickup::PICKUP_TYPE_NONE) + 1 , ((int)WDSPickup::PICKUP_TYPE_GUNSTART) - 1);			
		}

		PickupTYpe =   WDSPickup::PICKUP_TYPES(RandVal);
		
		
	}
	else
	{
		// should I spawn Weapon Pickup???
		if(m_pPlayer->GetIsOnlyDefaultGun())
		{
			// we already spawn weapon pickup and still active?
			if(IsGunPickupActive())
			{

			}
			else
			{	
				// spawn NOT so adavanced gun
				RandVal = num::RAND32::Instance().RandInt( ((int)WDSPickup::PICKUP_TYPE_GUNSTART) + 1 , ((int)WDSPickup::PICKUP_TYPE_GAUSSGUN) - 1);
				PickupTYpe =   WDSPickup::PICKUP_TYPES(RandVal);
			}
					
		}
	}

	if( PickupTYpe != WDSPickup::PICKUP_TYPE_NONE)
		Spawn(PickupTYpe, Pos);

}

bool	WDSPickupManager::IsGunPickupActive() const
{
	WDSPickup::PICKUP_TYPES PickupType = WDSPickup::PICKUP_TYPE_NONE;
	for(int i = 0; i < (int) m_PickupCollections.size(); i++)
	{
		for(int j = 0; j < (int) m_PickupCollections[i]->GetCount(); j++)
		{
			WDSPickup* pPickup = m_PickupCollections[i]->GetPickupByIndex(j);
			if(pPickup && pPickup->IsAlive())
			{
				PickupType = pPickup->GetType();
				if(PickupType > WDSPickup::PICKUP_TYPE_GUNSTART && PickupType < WDSPickup::PICKUP_TYPE_GUNEND)
					return true;
			}
		}
	}

	return false;
}
